package model;

import java.util.ArrayList;
import java.util.HashMap;

public class CategoryManager {
	private ArrayList<String> uniqueCategories; // every unique category in the order they were added to the Hash
	private HashMap<String, int[]> categoricalArraysMap;
	
	public CategoryManager()
	{
		uniqueCategories = new ArrayList<String>();
		categoricalArraysMap = new HashMap<String, int[]>();
	}
	
	// In this version of addElement, we keep track of unique instances of each category
	// countainsIndexes is an array that lists out every index that is part of that category
	public void addElement(String e, int index)
	{
	    Object countainsIndexes = categoricalArraysMap.get(e);
	    if(countainsIndexes != null) {
	      // append the new element to the end of the array:
	      int[] tmpCount = (int[])countainsIndexes;
	      countainsIndexes = new int[tmpCount.length+1];
	      for(int i = 0; i < tmpCount.length; i++){
	    	  ((int[])(countainsIndexes))[i] = tmpCount[i];
	      }
	      ((int[])(countainsIndexes))[tmpCount.length] = index; // do the actual append
	      categoricalArraysMap.put(e, (int[])countainsIndexes); // add the array back into the hashmap. (might not be necessary)
	    }
	    else {
	      categoricalArraysMap.put(e, new int[]{index}); // start off a new array for this new category
	      uniqueCategories.add(e);
	  }
	}
	
	public HashMap<String, int[]> getCategoryMap()
	{
		return categoricalArraysMap;
	}
	  
	// Use this method to determine what categories exist for this axis
	public ArrayList<String> getUniqueCategories()
	{
		return uniqueCategories;
	}
}
